#!/bin/bash

set -e

echo -e "\e[32m[1/5] 安装依赖和 Dante SOCKS5 服务...\e[0m"
sudo apt update
sudo apt install -y dante-server gcc make wget curl

echo -e "\e[32m[2/5] 创建用户 ss123...\e[0m"
sudo useradd -s /bin/false ss123
echo "ss123:ss123" | sudo chpasswd

echo -e "\e[32m[3/5] 配置 Dante...\e[0m"
sudo bash -c 'cat > /etc/danted.conf <<EOF
logoutput: /var/log/sockd.log
internal: 0.0.0.0 port = 16585
external: eth0
method: username
user.privileged: root
user.notprivileged: ss123

client pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    log: connect disconnect
}
pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    command: bind connect udpassociate
    log: error
    method: username
}
EOF'

echo -e "\e[32m[4/5] 启动服务...\e[0m"
sudo systemctl restart danted
sudo systemctl enable danted

echo -e "\e[32m[5/5] 测试代理...\e[0m"
IP=$(curl --socks5 127.0.0.1:16585 -U ss123:ss123 -s http://ifconfig.me || echo "失败")

if [[ "$IP" == "失败" || -z "$IP" ]]; then
    echo -e "\e[31m❌ 代理测试失败，请检查日志：sudo tail -f /var/log/sockd.log\e[0m"
else
    echo -e "\e[32m✅ 代理工作正常，当前出口IP为：$IP\e[0m"
fi

echo -e "\n\e[1;36mSOCKS5 代理安装完成！\e[0m"
echo -e "\e[33m用户名: ss123  密码: ss123\e[0m"
echo -e "\e[33m端口: 16585\e[0m"
echo -e "\e[33m修改密码: sudo passwd ss123\e[0m"
echo -e "\e[33m更改端口  : sudo nano /etc/danted.conf \e[0m"
echo -e "\e[33m测试命令: curl --socks5 127.0.0.1:16585 -U ss123:ss123 http://ifconfig.me\e[0m"